package database

import (
	"JoRedis/config"
	"JoRedis/interface/redis"
	"JoRedis/interface/reply"
	"JoRedis/protocol"
	"strconv"
)

func ExecuteSelect(redisConn redis.RedisConnItf, cmdBytesSlc [][]byte) reply.ReplyItf {
	dbIndex, err := strconv.Atoi(string(cmdBytesSlc[1]))
	if err != nil {
		return protocol.MakeErrReply("ERR invalid DB index")
	}
	if dbIndex >= config.PropPtr.Databases && dbIndex < 0 {
		return protocol.MakeErrReply("ERR DB index is out of range")
	}
	redisConn.SelectDBIdx(dbIndex)
	return protocol.MakeOkReply()
}
